package com.yy.controller;

import com.yy.entity.SysUser;
import com.yy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/user")
public class SysUserController {
    SysUserService service;
    @Autowired
    public SysUserController(SysUserService service){
        this.service=service;
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<SysUser> list=service.getAll();
        model.addAttribute( "list",list);//model打包成list属性完成list对象的值传递
        return "/user/list";
    }
    @RequestMapping(value = "/insert")
    public String insert(Model model){
        model.addAttribute("user",new SysUser());
        return "/user/edit";
    }
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id")Long id, Model model){
        SysUser entity=service.getById(id);
        model.addAttribute("user",entity);
        return "/user/edit";
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("user") SysUser entity) {
        boolean result = entity.getId() != null ? service.update(entity) : service.insert(entity);
        //以下return实现需要对result进行判断，当result为非0,页面跳转到list，否......
        return "redirect:/user/list";
    }
    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id") Long id, Model model) {
        SysUser entity = service.getById(id);
        model.addAttribute("user", entity);
        return "/user/detail";
    }
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        boolean result = service.delete(id);
        return "redirect:/user/list";
    }
}